package connection;

import java.util.*;

import bean.*;

public class Service {

	DatabaseConnection db = new DatabaseConnection();
	
	public String signUp(String emailId, String password, String firstName, String lastName) {
		String result = null;
		
		//System.out.println("harry1");
		
		result = db.addUser(emailId, password, firstName, lastName);
		
		//result = d
		
		return result;
		
	}
	
	public String signIn(String emailId, String password) {
		String result = null;
		
		result = db.retreiveUser(emailId, password);
		
		return result;
		
	}
	
	public String addAdvertisement(String userId, String itemName, String itemDescription, String itemPrice, String itemQuantity, String sellerInfo) {
		
		String result = null;
		
		result = db.addAdvertisement(userId, itemName, itemDescription,itemPrice,itemQuantity,sellerInfo);
		
		return result;
	}
	
	public Advertisement[] getMyAdvertisement(String userId) {
		
		//String result = null;
		ArrayList<Advertisement> alAdvert = new ArrayList<Advertisement>();
		
		//Advertisement oAdvert = new Advertisement();
		
		alAdvert = db.fetchAdvertisementById(userId);
		
		Advertisement aoAdvert[] = new Advertisement[alAdvert.size()];
		
		Iterator<Advertisement> itr = alAdvert.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoAdvert[i] = (Advertisement)itr.next();
			System.out.println(aoAdvert[i].getItemName());
			i++;
		}
		
		System.out.println(alAdvert.size());
		//result = db.fetchAdvertisementById(userId);
		
		return aoAdvert;
		
	}
	
	public Advertisement[] getOtherAdvertisement(String userId) {
		
		//String result = null;
		ArrayList<Advertisement> alAdvert = new ArrayList<Advertisement>();
		
		//Advertisement oAdvert = new Advertisement();
		
		alAdvert = db.fetchAdvertisementExcludingId(userId);
		
		Advertisement aoAdvert[] = new Advertisement[alAdvert.size()];
		
		Iterator<Advertisement> itr = alAdvert.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoAdvert[i] = (Advertisement)itr.next();
			System.out.println(aoAdvert[i].getItemName());
			i++;
		}
		
		System.out.println(alAdvert.size());
		//result = db.fetchAdvertisementById(userId);
		
		return aoAdvert;
		
	}
	
	public String addItemToCart(String userId,String itemId, String itemQuantity) {
		
		String result = null;
		result = db.insertCartItem(userId,itemId,itemQuantity);
		
		return result;
		
	}
	
	public CartItem[] getUserCart(String userId) {
		
		ArrayList<CartItem> alCartItem = new ArrayList<CartItem>();
		
		//Advertisement oAdvert = new Advertisement();
		
		alCartItem = db.fetchUserCart(userId);
		
		CartItem aoCartItem[] = new CartItem[alCartItem.size()];
		
		Iterator<CartItem> itr = alCartItem.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoCartItem[i] = (CartItem)itr.next();
			//System.out.println(aoCartItem[i].getItemName());
			i++;
		}
		
		System.out.println(alCartItem.size());
		//result = db.fetchAdvertisementById(userId);
		
		return aoCartItem;
		
	}
	
	public String updateItemInCart(String cartItemId, String orderedQuantity) {
		String result = null;
		
		result = db.updateCartItem(cartItemId,orderedQuantity);
		
		return result;
	}
	
	public Advertisement[] getUserCartItemDetails(String userId) {
		
		ArrayList<Advertisement> alAdvert = new ArrayList<Advertisement>();
		
		//Advertisement oAdvert = new Advertisement();
		
		alAdvert = db.fetchAdvertisementDetailsOfCartItems(userId);
		
		Advertisement aoAdvert[] = new Advertisement[alAdvert.size()];
		
		Iterator<Advertisement> itr = alAdvert.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoAdvert[i] = (Advertisement)itr.next();
			System.out.println(aoAdvert[i].getItemName());
			i++;
		}
		
		System.out.println(alAdvert.size());
		//result = db.fetchAdvertisementById(userId);
		
		return aoAdvert;
	}
	
	public String deleteItemInCart(String cartItemId) {
		String result = null;
		
		result = db.deleteItemInCart(cartItemId);
		
		return result;
		
	}
	
	public String checkAvailableQuantityForItemsInCart(String userId) {
		
		String result = null;
		
		result = db.checkAvailableQuantityForItemsInCart(userId);
		
		return result;
		
	}
	
	public String purchaseItemsInCart(String userId) {
		String result = "false";
		
		result = db.purchaseItemsInCart(userId);
		return result;
	}
	
	public TransactedItem[] getPurchasedItems(String userId) {
		
		ArrayList<TransactedItem> alTransactedItem = new ArrayList<TransactedItem>();
		
		
		alTransactedItem = db.getPurchasedItems(userId);
		
		TransactedItem aoTransactedItem[] = new TransactedItem[alTransactedItem.size()];
		
		Iterator<TransactedItem> itr = alTransactedItem.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoTransactedItem[i] = (TransactedItem)itr.next();
			//System.out.println(aoTransactedItem[i].getItemName());
			i++;
		}
		
		System.out.println(alTransactedItem.size());
		//result = db.fetchAdvertisementById(userId);
		aoTransactedItem.toString()
		return aoTransactedItem;
	}
	public TransactedItem[] getSoldItems(String userId) {
		
		ArrayList<TransactedItem> alTransactedItem = new ArrayList<TransactedItem>();
		
		
		alTransactedItem = db.getSoldItems(userId);
		
		TransactedItem aoTransactedItem[] = new TransactedItem[alTransactedItem.size()];
		
		Iterator<TransactedItem> itr = alTransactedItem.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoTransactedItem[i] = (TransactedItem)itr.next();
			//System.out.println(aoTransactedItem[i].getItemName());
			i++;
		}
		
		System.out.println(alTransactedItem.size());
		//result = db.fetchAdvertisementById(userId);
		
		return aoTransactedItem;
	}
	
	public Advertisement[] getOtherAdvertisementExcludingId(String userId) {
		
		//String result = null;
		ArrayList<Advertisement> alAdvert = new ArrayList<Advertisement>();
		
		//Advertisement oAdvert = new Advertisement();
		
		alAdvert = db.getOtherAdvertisementExcludingId(userId);
		
		Advertisement aoAdvert[] = new Advertisement[alAdvert.size()];
		
		Iterator<Advertisement> itr = alAdvert.iterator(); 
		int i=0;
		while(itr.hasNext()) {
			aoAdvert[i] = (Advertisement)itr.next();
			System.out.println(aoAdvert[i].getItemName());
			i++;
		}
		
		System.out.println(alAdvert.size());
		//result = db.fetchAdvertisementById(userId);
		
		return aoAdvert;
		
	}
	
	public String updateMyAdvertisement(String itemId,String itemQuantity) {
		
		String result = "false";
		
		result = db.updateMyAdvertisement(itemId,itemQuantity);
		
		return result;
	}
	
	public String deleteMyAdvertisement(String itemId) {
		
		String result = null;
		
		result = db.deleteMyAdvertisement(itemId);
		
		return result;
	}
	
}
